package com.sdl.timesblogmp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdl.timesblogmp.exception.CustomException;
import com.sdl.timesblogmp.model.entity.Category;
import com.sdl.timesblogmp.mapper.CategoryMapper;
import com.sdl.timesblogmp.model.vo.BlogInfo;
import com.sdl.timesblogmp.model.vo.PageParam;
import com.sdl.timesblogmp.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sdl
 * @since 2020-05-11
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;
    @Override
    public IPage<Category> findCategoryInfo(PageParam pageParam) {
        if (pageParam.getPageNo()==0||pageParam.getPageSize()==0){
            pageParam.setPageNo(1);
            pageParam.setPageSize(6);
        }
        QueryWrapper<Category> wrapper = new QueryWrapper();
        if (pageParam.getName()!=null){
            wrapper.like("name",pageParam.getName());
        }
        Page<Category> page = new Page<>(pageParam.getPageNo(),pageParam.getPageSize());
        page = categoryMapper.selectPage(page,wrapper);
        return page;
    }

    @Override
    public int delCategoru(Integer id) {
        Category category = categoryMapper.selectById(id);
        if (null!=category){
            int check = categoryMapper.checkCategoryDel(id);
            if (check!=0){
                throw new CustomException("分类包含关联数据，不能删除！");
            }
            return categoryMapper.deleteById(id);
        }else {
            throw new CustomException("分类不存在！");
        }
    }
}
